class_name RangePicker
extends ActivableControl

const TEXTURE_TICK_ON := preload("res://assets/art/ui/ui-tick-on.png")
const TEXTURE_TICK_OFF := preload("res://assets/art/ui/ui-tick-off.png")


signal value_change(value: int)


@onready var ticksContainer: BoxContainer = $TicksContainer

## 当前值，范围应该在 1 - 10 之间
@export_range(1, 10) var current_value: int

# 从 1 开始
var min_value: int
var max_value: int
var ticks: Array[TextureRect] = []


func _ready():
	super._ready()

	for tick: TextureRect in ticksContainer.get_children():
		ticks.push_back(tick)
	min_value = 1
	max_value = ticks.size()

	set_value(current_value)


func _process(_delta: float):
	process_input()



func set_value(value: int):
	current_value = clampi(value, min_value, max_value)
	refresh()
	value_change.emit(current_value)


func refresh():
	var current_value_index := current_value - 1
	for i in range(0, ticks.size()):
		if i <= current_value_index:
			ticks[i].texture = TEXTURE_TICK_ON
		else:
			ticks[i].texture = TEXTURE_TICK_OFF


func process_input():
	if not is_active:
		return
	var i = input_manager.left_right_input()
	if i == 0:
		return
	set_value(current_value + i)
